#!/usr/bin/env bash
#
# Collection of various test strings that could be the output of the tmux
# 'pane_current_comamnd' message. Included as regression test for updates to
# the inline grep pattern used in the `.tmux.conf` configuration

set -e

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
NORMAL=$(tput sgr0)

# Import 'vim_pattern'
source 'vim-tmux-navigator.tmux'

match_tests=(
  vim
  Vim
  VIM
  vimdiff
  lvim
  /usr/local/bin/vim
  vi
  gvim
  view
  gview
  nvim
  vimx
  fzf
  .vim-wrapped
  .nvim-wrapped
)
no_match_tests=(
  /Users/christoomey/.vim/thing
  /usr/local/bin/start-vim
)

MATCH_RESULT="${GREEN}match${NORMAL}"
NO_MATCH_RESULT="${RED}not match${NORMAL}"

display_matches() {
  local result
  local final_status=0
  for process_name in "$@"; do
    result="$(matches_vim_pattern $process_name)"
    if [[ "${result}" != "${expect_result}" ]]; then
      final_status=1
    fi
    printf "%s %s\n" "${result}" "$process_name"
  done
  return "${final_status}"
}

matches_vim_pattern() {
  if echo "$1" | grep -iqE "^${vim_pattern}$"; then
    echo "${MATCH_RESULT}"
  else
    echo "${NO_MATCH_RESULT}"
  fi
}

main() {
  echo -e "Testing against pattern: ${YELLOW}$vim_pattern${NORMAL}\n"

  local final_status=0
  echo -e "These should all ${MATCH_RESULT}\n----------------------"
  local expect_result="${MATCH_RESULT}"
  display_matches "${match_tests[@]}" || final_status=1

  echo -e "\nThese should all ${NO_MATCH_RESULT}\n--------------------------"
  expect_result="${NO_MATCH_RESULT}"
  display_matches "${no_match_tests[@]}" || final_status=1

  if [[ "${final_status}" == 0 ]]; then
    echo -e "\n${GREEN}All test cases passed!${NORMAL}"
  else
    echo -e "\n${RED}Some test cases are failing${NORMAL}"
  fi
  return "${final_status}"
}

main
